SwordFighter = {}
SwordFighter.__index = SwordFighter
 
function SwordFighter.new()
    local instance = {
      x1 = nil,
      y1 = nil,
      x2 = nil,
      y2 = nil,
      ownerObject = nil,
      hasMoved = nil,
      hasAttacked = false,
      health = 10,
      damage = 1.5,
      attackRange = 1,
      resetAttackRange = 1,
      moveRange = 2,
      healthTextbox = nil,
      tileUnitOn = nil,
      isHero = false
    }
    
    setmetatable(instance, SwordFighter)
    return instance
end

setmetatable(SwordFighter,{__index = Unit})

function SwordFighter:getIsHero()
  return self.isHero
end

function SwordFighter:setIsHero(_hero)
  self.isHero = _hero
end

function SwordFighter:setTileUnitOn(_tile)
  self.tileUnitOn = _tile
end

function SwordFighter:getTileUnitOn()
  return self.tileUnitOn
end

function SwordFighter:getHasAttacked()
  return self.hasAttacked
end

function SwordFighter:setHasAttacked(_att)
  self.hasAttacked = _att
end

function SwordFighter:getHealthBox()
  return self.healthTextbox
end

function SwordFighter:moveHealthBox(x, y)
  self.healthTextbox:moveLoc(x,y,0,2)
end

function SwordFighter:updateHealthBox()
  self.healthTextbox:setString(tostring(self.health))
end

function SwordFighter:decreaseHealthBy(_health, _clickedUnitProp)
  local temp = self.health
  self.health = temp - _health
  
  if self.health <= 0 then
    self:killUnit(_clickedUnitProp)
    return true
  else 
    return false
  end
end

function SwordFighter:increaseHealthBy(_health)
  local temp = self.health
  self.health = temp + _health
end

function SwordFighter:getHealth()
  return self.health
end

function SwordFighter:getHasMoved()
  return self.hasMoved
end

function SwordFighter:setHasMoved(_hasMoved)
  self.hasMoved = _hasMoved
end

function SwordFighter:getOwnerObject()
  return self.ownerObject
end

function SwordFighter:setOwnerObject(_owObj)
  self.ownerObject = _owObj
end

function SwordFighter:getRectangleValues()
  return self.x1, self.y1, self.x2, self.y2
end

function SwordFighter:setRectangleValues(_x1, _y1, _x2, _y2)
  self.x1 = _x1
  self.y1 = _y1
  self.x2 = _x2
  self.y2 = _y2
end

function SwordFighter:spawnUnit(x1, y1, x2, y2)
  local spriteUnit = MOAIGfxQuad2D.new()
  
  if self.ownerObject == pl1 then
    spriteUnit:setRect(x1, y1, x2, y2)
    spriteUnit:setTexture(textureSwordRed)
  else
    spriteUnit:setRect(x2, y1, x1, y2)
    spriteUnit:setTexture(textureSwordBlue)
  end
  
  local propUnit = MOAIProp2D.new()
  propUnit:setPriority(30)
  propUnit:setDeck(spriteUnit)
  propUnit:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)
  
  MainLayer:insertProp(propUnit)
  
  propToTileObject[propUnit] = self
  
  currentPlayerTurn:decreaseGold(150)
  
  self.healthTextbox = MOAITextBox.new()
  self.healthTextbox:setStyle(styleHealthCaptureBox)
  self.healthTextbox:setRect(self.x1 + 36 , self.y1 + 45, self.x2 + 10, self.y2 + 45)
  self.healthTextbox:setAlignment(MOAITextBox.RIGHT_JUSTIFY, MOAITextBox.RIGHT_JUSTIFY)
  self.healthTextbox:setYFlip(true)
  self.healthTextbox:setString(tostring(self.health))
  self.healthTextbox:setPriority(30)
  MainLayer:insertProp(self.healthTextbox)
  unitHealthBoxRenderTable[table.getn(unitHealthBoxRenderTable) + 1] = self.healthTextbox
end

function SwordFighter:killUnit(_clickedUnitProp)
  for k, v in pairs(unitHealthBoxRenderTable) do
    if v == self.healthTextbox then
      MainLayer:removeProp(v)
      table.remove(unitHealthBoxRenderTable, k)
      break
    end
  end
  self.tileUnitOn:setUnit(nil)
  self.tileUnitOn = nil
  propToTileObject[_clickedUnitProp] = nil
  self:getOwnerObject():removeunitFromList(_clickedUnitProp)
  MainLayer:removeProp(_clickedUnitProp)
end

function SwordFighter:getType()
  return "SwordFighter"
end

function SwordFighter:getBuildingModifier(_buildingType)
  if _buildingType == "Castle-player1" then 
    return 1.8
  elseif _buildingType == "Castle-player2" then
    return 1.8
  elseif _buildingType == "Castle-neutral" then
    return 1.8
  elseif _buildingType == "BarracksCavalry-player1" then
    return 1.4
  elseif _buildingType == "BarracksCavalry-player2" then
    return 1.4
  elseif _buildingType == "BarracksCavalry-neutral" then
    return 1.4
  elseif _buildingType == "BarracksMelee-player1" then
    return 1.4
  elseif _buildingType == "BarracksMelee-player2" then
    return 1.4
  elseif _buildingType == "BarracksMelee-neutral" then
    return 1.4
  elseif _buildingType == "BarracksRange-player1" then
    return 1.4
  elseif _buildingType == "BarracksRange-player2" then
    return 1.4
  elseif _buildingType == "BarracksRange-neutral" then
    return 1.4
  elseif _buildingType == "Headquarters-player1" then
    return 1.6
  elseif _buildingType == "Headquarters-player2" then
    return 1.6
  elseif _buildingType == "CapturePointBuilding-neutral" then
    return 1.2
  elseif _buildingType == "CapturePointBuilding-player1" then
    return 1.2
  elseif _buildingType == "CapturePointBuilding-player2" then
    return 1.2
  end
end

function SwordFighter:getUnitModifier(_unitType)
  if _unitType == "Heavy_Horse" then
    return 1.4
  elseif _unitType == "Archer" then
    return 1.8
  elseif _unitType == "SwordFighter" then
    return 1.4
  end    
end

function SwordFighter:getTerrainModifier(_terrain)
  if _terrain == "Grass" then
    return 1.5
  elseif _terrain == "Bridge" then
    return 1.4
  elseif _terrain == "Forrest1" then
    return 1.8
  elseif _terrain == "Forrest2" then
    return 1.8
  elseif _terrain == "Big_Rocks" then
    return 1.7
  elseif _terrain == "Road_Horizontal" then
    return 1.4
  elseif _terrain == "Road_Loop1" then
    return 1.4
  elseif _terrain == "Road_Loop2" then
    return 1.4
  elseif _terrain == "Road_Loop3" then
    return 1.4
  elseif _terrain == "Road_Loop4" then
    return 1.4
  elseif _terrain == "Road_Split1" then
    return 1.4
  elseif _terrain == "Road_Split2" then
    return 1.4
  elseif _terrain == "Road_Split3" then
    return 1.4
  elseif _terrain == "Road_Split4" then
    return 1.4
  elseif _terrain == "Road_Vertical" then
    return 1.4
  end
end

function SwordFighter:getDamage()
  return (self.damage * self.health) / 10
end

function SwordFighter:setDamageMultiplier(_damage)
  local temp = self.damage
  self.damage = (temp * _damage)
end

function SwordFighter:attackReset()
  self.attackRange = self.resetAttackRange
end

function SwordFighter:setAttackRange(_range)
  self.attackRange = _range
  if alsoUpdateResetValue == true then
    self.resetAttackRange = _range
  end
end

function SwordFighter:setMoveRange(_range)
  self.moveRange = _range
end

function SwordFighter:getAttackRange()
  return self.attackRange
end

function SwordFighter:getMoveRange()
  return self.moveRange
end
 
return SwordFighter